﻿function insert(position, user, request) {

    position.CreatedAt = new Date();

    request.execute({
        success: function() {
            
            request.respond();
            
            var devicesTable = tables.getTable("Devices");
            
            devicesTable               
                .where({ id: position.DeviceId })
                .read({
                    success: function(devices) {
                        if (devices.length > 0) {
                            var device = devices[0];
                            device.Coordinate = position.Coordinate;
                            devicesTable.update(device);
                        }
                    } 
                 });
            }
    });
}